package com.xique.door.bean.transfer;

import com.xique.common.core.utils.OrikaUtil;
import com.xique.door.bean.domain.Area;
import com.xique.door.bean.domain.BuildArea;
import com.xique.door.bean.domain.BuildBuilding;
import com.xique.door.bean.domain.Elevator;
import com.xique.door.bean.vo.elevator.ElevatorVo;
import com.xique.door.service.IAreaService;
import com.xique.door.service.IBuildAreaService;
import com.xique.door.service.IBuildBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: caogq
 * @Date: 2021/7/26 11:57
 * @Description:
 */
@Component
public class ElevatorTransfer {
    @Autowired
    private IBuildAreaService buildAreaService;

    @Autowired
    private IBuildBuildingService buildBuildingService;

    @Autowired
    private IAreaService areaService;

    public List<ElevatorVo> elevatorTransferElevatorVos(List<Elevator> list) {

        List<Long> buildingIds = list.stream().map(Elevator::getBuildingId).collect(Collectors.toList());
        List<BuildBuilding> buildingList = buildBuildingService.selectBuildBuildingListByIds(buildingIds);
        Map<Long, String> buildingNameMap = buildingList.stream().collect(Collectors.toMap(BuildBuilding::getId, BuildBuilding::getBuildingName));

        List<ElevatorVo> vos = new ArrayList<>(list.size());
        list.forEach(item -> {
            ElevatorVo vo = OrikaUtil.convert(item, ElevatorVo.class);
            vo.setBuildingName(buildingNameMap.get(item.getBuildingId()));
            vos.add(vo);
        });

        return vos;
    }

    public ElevatorVo elevatorTransferElevatorVo(Elevator elevator) {
        ElevatorVo elevatorVo = OrikaUtil.convert(elevator, ElevatorVo.class);

        BuildArea buildArea = buildAreaService.selectBuildAreaById(elevator.getAreaId());
        if (buildArea!= null ){
            elevatorVo.setAreaName(buildArea.getAreaName());
        }

        BuildBuilding buildBuilding = buildBuildingService.selectBuildBuildingById(elevator.getBuildingId());
        if (buildBuilding!= null ){
            elevatorVo.setBuildingName(buildBuilding.getBuildingName());
        }

        Area province = areaService.selectAreaById(elevator.getProvinceId());
        Area city = areaService.selectAreaById(elevator.getCityId());
        Area district = areaService.selectAreaById(elevator.getDistrictId());
        if (province != null) {
            elevatorVo.setProvinceName(province.getAreaName());
        }
        if (city != null) {
            elevatorVo.setCityName(city.getAreaName());
        }
        if (district != null) {
            elevatorVo.setDistrictName(district.getAreaName());
        }
        return elevatorVo;
    }

}
